package com.cty.tree;
/*
*
* 二叉树节点
*
*
* */
public class HeroNode {
    public int id;
    public String name;
    public HeroNode left;
    public HeroNode right;
    //为了线索化二叉树
    public int leftNodeType;//左结点的类型0:表示左子结点为左子树，1表示为中序左索引
    public int rightNodeType;//右结点类型0:表示右子结点为左子树，1表示为中序右索引
    public HeroNode(int id,String name) {
        this.id = id;
        this.name = name;
    }

    //前序遍历方法
    public void proOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.proOrder();
        }
        if (this.right != null) {
            this.right.proOrder();
        }
    }

    //中序遍历
    public void midOrder() {
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //前序查找
    public HeroNode preOrderSearch(int no) {
        System.out.println("进入前序查找");
        if (this.id == no) {
            return this;
        }
        HeroNode resNode = null;
        //向左遍历
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //向右遍历
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序查找
    public HeroNode midOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.midOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("进入中序查找");
        if (this.id == no) {
            return this;
        }
        if (this.right != null) {
            resNode = this.right.midOrderSearch(no);
        }
        return resNode;
    }

    //后序查找
    public HeroNode postOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) {
            return  resNode;
        }
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {
            return  resNode;
        }
        System.out.println("进入后序查找");
        if (this.id == no) {
            return this;
        } else {
            return null;
        }
    }

    //删除方法
    public void delNode(int no) {
        if (this.left != null && this.left.id == no) {
            //左右两个节点都有的情况
            if (this.left.left != null && this.left.right != null) {
                this.left.left.right = this.left.right;
                this.left = this.left.left;
                return;
            } else if (this.left.left != null || this.left.right == null) {
                this.left = this.left.left;
                return;
            } else if (this.left.left == null || this.left.right != null) {
                this.left = this.left.right;
                return;
            } else {
                this.left = null;
                return;
            }

        }
        if (this.right != null && this.right.id == no) {
            if (this.right.left != null && this.right.right != null) {
                this.right.left.right = this.right.right;
                this.right = this.right.left;
                return;
            } else if (this.right.left != null && this.right.right == null) {
                this.right = this.right.left;
                return;
            } else if (this.right.left == null && this.right.right != null) {
                this.right = this.right.right;
                return;
            } else {
                this.right = null;
            }
        }
        if (this.left != null) {
            this.left.delNode(no);
        }
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
